<template>
	<view class="container">
		<view class="form-box">
			<!-- 文件上传区域 -->
			<view class="upload-section">
				<view class="upload-title">
					<text class="label">打印文件</text>
					<text class="tip">(支持PDF、Word、PPT等格式)</text>
				</view>
				<view class="file-list" v-if="formData.files.length > 0">
					<view class="file-item" v-for="(file, index) in formData.files" :key="index">
						<view class="file-info">
							<text class="file-name">{{file.name}}</text>
							<text class="file-size">{{file.size}}MB</text>
						</view>
						<view class="file-actions">
							<text class="delete-btn" @tap="deleteFile(index)">删除</text>
						</view>
					</view>
				</view>
				<view class="upload-btn" @tap="chooseFile">
					<uni-icons type="upload" size="32" color="#2979ff"></uni-icons>
					<text>点击上传文件</text>
				</view>
			</view>

			<!-- 打印设置 -->
			<view class="print-settings">
				<view class="form-item">
					<text class="label">打印份数</text>
					<view class="counter">
						<text class="minus" @tap="adjustCopies('minus')">-</text>
						<input type="number" v-model="formData.copies" />
						<text class="plus" @tap="adjustCopies('plus')">+</text>
					</view>
				</view>

				<view class="form-item">
					<text class="label">打印颜色</text>
					<radio-group @change="onColorChange">
						<label class="radio-item" v-for="(item, index) in colorOptions" :key="index">
							<radio :value="item.value" :checked="formData.color === item.value" color="#2979ff" />
							<text>{{item.label}}</text>
						</label>
					</radio-group>
				</view>

				<view class="form-item">
					<text class="label">打印规格</text>
					<radio-group @change="onSizeChange">
						<label class="radio-item" v-for="(item, index) in sizeOptions" :key="index">
							<radio :value="item.value" :checked="formData.size === item.value" color="#2979ff" />
							<text>{{item.label}}</text>
						</label>
					</radio-group>
				</view>

				<view class="form-item">
					<text class="label">双面打印</text>
					<switch :checked="formData.doubleSided" @change="onDoubleSidedChange" color="#2979ff" />
				</view>
			</view>

			<!-- 配送信息 -->
			<view class="delivery-info">
				<view class="form-item">
					<text class="label">送达地点</text>
					<view class="location-select" @tap="selectLocation">
						<text>{{formData.location || '请选择送达地点'}}</text>
						<uni-icons type="right" size="16" color="#999"></uni-icons>
					</view>
				</view>

				<view class="form-item">
					<text class="label">期望送达时间</text>
					<picker mode="time" :value="formData.expectedTime" @change="onTimeChange">
						<view class="picker">
							<text>{{formData.expectedTime || '请选择时间'}}</text>
							<uni-icons type="right" size="16" color="#999"></uni-icons>
						</view>
					</picker>
				</view>

				<view class="form-item">
					<text class="label">备注信息</text>
					<textarea v-model="formData.remark" placeholder="请输入备注信息（选填）" />
				</view>
			</view>

			<!-- 费用明细 -->
			<view class="price-section">
				<view class="price-item">
					<text class="label">打印费用</text>
					<text class="price">¥{{printFee}}</text>
				</view>
				<view class="price-item">
					<text class="label">配送费</text>
					<text class="price">¥{{deliveryFee}}</text>
				</view>
				<view class="price-item">
					<text class="label">加急服务</text>
					<switch :checked="formData.isUrgent" @change="onUrgentChange" color="#2979ff" />
				</view>
				<view class="price-item total">
					<text class="label">总计</text>
					<text class="price">¥{{totalPrice}}</text>
				</view>
			</view>
		</view>

		<!-- 发布按钮 -->
		<view class="submit-btn" @tap="submitOrder">
			发布订单
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			formData: {
				files: [],
				copies: 1,
				color: 'black',
				size: 'A4',
				doubleSided: false,
				location: '',
				expectedTime: '',
				remark: '',
				isUrgent: false
			},
			colorOptions: [
				{ label: '黑白', value: 'black' },
				{ label: '彩色', value: 'color' }
			],
			sizeOptions: [
				{ label: 'A4', value: 'A4' },
				{ label: 'A3', value: 'A3' }
			],
			priceRates: {
				black: { A4: 0.2, A3: 0.4 },
				color: { A4: 1, A3: 2 }
			},
			deliveryFee: 3,
			urgentFee: 5
		}
	},
	computed: {
		printFee() {
			const baseRate = this.priceRates[this.formData.color][this.formData.size]
			let fee = baseRate * this.formData.copies
			if (this.formData.doubleSided) {
				fee *= 1.5
			}
			return Number(fee.toFixed(2))
		},
		totalPrice() {
			return Number((this.printFee + this.deliveryFee + 
				(this.formData.isUrgent ? this.urgentFee : 0)).toFixed(2))
		}
	},
	methods: {
		chooseFile() {
			// 模拟文件选择
			const mockFile = {
				name: '课程作业.pdf',
				size: '2.5'
			}
			this.formData.files.push(mockFile)
		},
		deleteFile(index) {
			this.formData.files.splice(index, 1)
		},
		adjustCopies(type) {
			if (type === 'minus' && this.formData.copies > 1) {
				this.formData.copies--
			} else if (type === 'plus') {
				this.formData.copies++
			}
		},
		onColorChange(e) {
			this.formData.color = e.detail.value
		},
		onSizeChange(e) {
			this.formData.size = e.detail.value
		},
		onDoubleSidedChange(e) {
			this.formData.doubleSided = e.detail.value
		},
		selectLocation() {
			uni.showActionSheet({
				itemList: ['宿舍楼', '教学楼', '图书馆', '食堂'],
				success: (res) => {
					this.formData.location = ['宿舍楼', '教学楼', '图书馆', '食堂'][res.tapIndex]
				}
			})
		},
		onTimeChange(e) {
			this.formData.expectedTime = e.detail.value
		},
		onUrgentChange(e) {
			this.formData.isUrgent = e.detail.value
		},
		submitOrder() {
			// 表单验证
			if (this.formData.files.length === 0) {
				uni.showToast({
					title: '请上传打印文件',
					icon: 'none'
				})
				return
			}
			if (!this.formData.location) {
				uni.showToast({
					title: '请选择送达地点',
					icon: 'none'
				})
				return
			}
			if (!this.formData.expectedTime) {
				uni.showToast({
					title: '请选择期望送达时间',
					icon: 'none'
				})
				return
			}

			// TODO: 发送请求保存订单
			uni.showLoading({
				title: '发布中...'
			})

			setTimeout(() => {
				uni.hideLoading()
				uni.showToast({
					title: '发布成功',
					icon: 'success'
				})
				// 返回首页
				uni.switchTab({
					url: '/pages/index/index'
				})
			}, 1500)
		}
	}
}
</script>

<style lang="scss">
.container {
	min-height: 100vh;
	background-color: $background-color;
	padding-bottom: 120rpx;
}

.form-box {
	background-color: #fff;
	padding: 20rpx;
	margin-top: 20rpx;
}

.upload-section {
	margin-bottom: 30rpx;

	.upload-title {
		margin-bottom: 20rpx;

		.label {
			font-size: 28rpx;
			color: $text-color;
		}

		.tip {
			font-size: 24rpx;
			color: #999;
			margin-left: 20rpx;
		}
	}

	.file-list {
		margin-bottom: 20rpx;

		.file-item {
			display: flex;
			justify-content: space-between;
			align-items: center;
			background-color: #f5f5f5;
			padding: 20rpx;
			border-radius: 8rpx;
			margin-bottom: 20rpx;

			.file-info {
				.file-name {
					font-size: 28rpx;
					color: $text-color;
				}

				.file-size {
					font-size: 24rpx;
					color: #999;
					margin-left: 20rpx;
				}
			}

			.delete-btn {
				color: #ff5500;
				font-size: 28rpx;
			}
		}
	}

	.upload-btn {
		height: 200rpx;
		background-color: #f5f5f5;
		border-radius: 8rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		
		text {
			font-size: 28rpx;
			color: #999;
			margin-top: 20rpx;
		}
	}
}

.print-settings, .delivery-info {
	margin-bottom: 30rpx;

	.form-item {
		margin-bottom: 30rpx;

		.label {
			font-size: 28rpx;
			color: $text-color;
			margin-bottom: 20rpx;
			display: block;
		}

		.counter {
			display: flex;
			align-items: center;
			background-color: #f5f5f5;
			padding: 10rpx;
			border-radius: 8rpx;
			width: 240rpx;

			input {
				flex: 1;
				text-align: center;
				font-size: 28rpx;
			}

			.minus, .plus {
				width: 60rpx;
				height: 60rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				font-size: 36rpx;
				color: $theme-color;
			}
		}

		.radio-item {
			margin-right: 40rpx;
			font-size: 28rpx;
		}

		.location-select, .picker {
			width: 100%;
			background-color: #f5f5f5;
			padding: 20rpx;
			border-radius: 8rpx;
			font-size: 28rpx;
			color: $text-color;
			display: flex;
			justify-content: space-between;
			align-items: center;
		}

		textarea {
			width: 100%;
			height: 160rpx;
			background-color: #f5f5f5;
			padding: 20rpx;
			border-radius: 8rpx;
			font-size: 28rpx;
		}
	}
}

.price-section {
	background-color: #fff;
	padding: 20rpx;
	margin-top: 20rpx;

	.price-item {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 20rpx;

		.label {
			font-size: 28rpx;
			color: $text-color;
		}

		.price {
			font-size: 32rpx;
			color: #ff5500;
			font-weight: bold;
		}

		&.total {
			margin-top: 30rpx;
			padding-top: 20rpx;
			border-top: 1rpx solid $border-color;
		}
	}
}

.submit-btn {
	position: fixed;
	bottom: 0;
	left: 0;
	right: 0;
	height: 100rpx;
	background-color: $theme-color;
	color: #fff;
	font-size: 32rpx;
	display: flex;
	align-items: center;
	justify-content: center;
}
</style> 